//
//  MineInfoEditEndTableViewCell.m
//  MJApp
//
//  Created by scjh on 15/3/27.
//  Copyright (c) 2015年 wangliang. All rights reserved.
//

#import "MineInfoEditEndTableViewCell.h"
#import "UITextView+WLCategory.h"

@interface MineInfoEditEndTableViewCell ()<UITextViewDelegate>
@property (weak, nonatomic) IBOutlet UITextView *cellTextView;
@property (weak, nonatomic) IBOutlet UILabel *perLabel;

@end

@implementation MineInfoEditEndTableViewCell

+ (instancetype)initWithTableView:(UITableView *)tableView {
    
    static NSString *cellID = @"MineInfoEditEndTableViewCell";
    MineInfoEditEndTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellID];
    if (cell == nil) {
        
        cell =  WL_XIB(@"MineInfoEditEndTableViewCell");
        
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
    }
    cell.cellHigh = CGRectGetHeight(cell.frame);
    return cell;
    
}

- (void)setInfoDic:(NSDictionary *)infoDic {
    
    _infoDic = infoDic;
    
    self.cellTextView.delegate = self;
    self.perLabel.text = infoDic[@"desc"];
    if (infoDic[@"value"]) {
        self.cellTextView.text = infoDic[@"value"];
        self.perLabel.hidden = YES;
    }
}

- (void)textViewDidChange:(UITextView *)textView {
    
    [textView textViewDidChange:textView];
}

- (BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text {
    
    BOOL boo = [textView textView:textView shouldChangeTextInRange:range replacementText:text];
    if (boo) {
        
        NSString *allString = nil;
        if (textView.text.length > 0) {
            
            if ([text isEqual:@""]) {
                
                //删除
                allString = [textView.text substringToIndex:textView.text.length - 1];
            }else {
                //添加
                if (range.length == 0) {
                    
                    allString = [NSString stringWithFormat:@"%@%@",textView.text,text];
                }else {
                    
                    allString = textView.text;
                }
            }
            
            NSMutableDictionary *dic = [NSMutableDictionary dictionaryWithDictionary:self.infoDic];
            [dic setObject:allString forKey:@"newString"];
            if ([self.delegate respondsToSelector:@selector(homeCell:withInfo:)]) {
                
                [self.delegate homeCell:self withInfo:dic];
            }
        }
        return YES;

    }else {
        
        return NO;
    }
}

- (void)awakeFromNib {
    // Initialization code
    
    UILabel *label = [[UILabel alloc] init];
    label.text = @"详情地址";
    label.textColor = [UIColor grayColor];
    self.cellTextView.textContainerInset = UIEdgeInsetsMake(10, 10, 10, 10);
    [self.cellTextView addPlaceholder:label];
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

- (void)dealloc {
    
    [self.cellTextView removeNotice];
}

@end
